package com.ape.mapsa.converters;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.ape.mapsa.produccion.model.Formula;
import com.ape.mapsa.produccion.service.IProduccionService;

@FacesConverter(value = "formula")
@ApplicationScoped
public class FormulasConverter implements Converter {

	@ManagedProperty(value = "#{produccionService}")
	private IProduccionService produccionService;
	public List<Formula> formulas;

	public FormulasConverter() {
	}

	@PostConstruct
	public void initialize() {
		formulas = new ArrayList<Formula>();
		formulas.addAll(produccionService.getAllFormulas());
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		for (Formula formula : formulas) {
			if (formula.getId().equals(Long.valueOf(value))) {
				return formula;
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		return value.toString();
	}

	public IProduccionService getProduccionService() {
		return produccionService;
	}

	public void setProduccionService(IProduccionService produccionService) {
		this.produccionService = produccionService;
	}

}
